package com.huaxia.controller.promotion;

import com.huaxia.pojo.promotion.*;
import com.huaxia.service.promotion.PromotionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.WebApplicationContext;

import javax.servlet.ServletContext;
import java.util.List;

/**
 * @author wangli 爱我华夏，爱我中华！
 * @date 2020/06/09 14:55
 */
@RestController
@RequestMapping("/promotion")
public class PromotionController {

    @Autowired
    private WebApplicationContext webApplicationContext;
    @Autowired
    private PromotionService promotionService;

    @GetMapping("/getBranchBC")
    public List<BranchBC> getBranchBC() {
        ServletContext servletContext = webApplicationContext.getServletContext();
        List<BranchBC> branchBCs = (List<BranchBC>) servletContext.getAttribute("branchBCs");
        if (branchBCs == null) {
            branchBCs = promotionService.getBranchBC();
            servletContext.setAttribute("branchBCs", branchBCs);
            return branchBCs;
        }
        return branchBCs;
    }

    @GetMapping("/getBranchSBC")
    public List<BranchSBC> getBranchSBC() {
        ServletContext servletContext = webApplicationContext.getServletContext();
        List<BranchSBC> branchSBCs = (List<BranchSBC>) servletContext.getAttribute("branchSBCs");
        if (branchSBCs == null) {
            branchSBCs = promotionService.getBranchSBC();
            servletContext.setAttribute("branchSBCs", branchSBCs);
            return branchSBCs;
        }
        return branchSBCs;
    }

    @GetMapping("/getBranchBM")
    public List<BranchBM> getBranchBM() {
        ServletContext servletContext = webApplicationContext.getServletContext();
        List<BranchBM> branchBMs = (List<BranchBM>) servletContext.getAttribute("branchBMs");
        if (branchBMs == null) {
            branchBMs = promotionService.getBranchBM();
            servletContext.setAttribute("branchBMs", branchBMs);
            return branchBMs;
        }
        return branchBMs;
    }

    @GetMapping("/getBranchSBM")
    public List<BranchSBM> getBranchSBM() {
        ServletContext servletContext = webApplicationContext.getServletContext();
        List<BranchSBM> branchSBMs = (List<BranchSBM>) servletContext.getAttribute("branchSBMs");
        if (branchSBMs == null) {
            branchSBMs = promotionService.getBranchSBM();
            servletContext.setAttribute("branchSBMs", branchSBMs);
            return branchSBMs;
        }
        return branchSBMs;
    }

    @GetMapping("/getBranchAS")
    public List<BranchAS> getBranchAS(){
        ServletContext servletContext = webApplicationContext.getServletContext();
        List<BranchAS> branchASs = (List<BranchAS>) servletContext.getAttribute("branchASs");
        if(branchASs==null){
            branchASs = promotionService.getBranchAS();
            servletContext.setAttribute("branchASs",branchASs);
            return branchASs;
        }
        return branchASs;
    }


    @GetMapping("/getSaleDeptBC")
    public List<SaleDeptBC> getsaleDeptBC() {
        ServletContext servletContext = webApplicationContext.getServletContext();
        List<SaleDeptBC> saleDeptBCs = (List<SaleDeptBC>) servletContext.getAttribute("saleDeptBCs");
        if (saleDeptBCs == null) {
            saleDeptBCs = promotionService.getSaleDeptBC();
            servletContext.setAttribute("saleDeptBCs", saleDeptBCs);
            return saleDeptBCs;
        }
        return saleDeptBCs;
    }

    @GetMapping("/getSaleDeptSBC")
    public List<SaleDeptSBC> getsaleDeptSBC() {
        ServletContext servletContext = webApplicationContext.getServletContext();
        List<SaleDeptSBC> saleDeptSBCs = (List<SaleDeptSBC>) servletContext.getAttribute("saleDeptSBCs");
        if (saleDeptSBCs == null) {
            saleDeptSBCs = promotionService.getSaleDeptSBC();
            servletContext.setAttribute("saleDeptSBCs", saleDeptSBCs);
            return saleDeptSBCs;
        }
        return saleDeptSBCs;
    }

    @GetMapping("/getSaleDeptBM")
    public List<SaleDeptBM> getsaleDeptBM() {
        ServletContext servletContext = webApplicationContext.getServletContext();
        List<SaleDeptBM> saleDeptBMs = (List<SaleDeptBM>) servletContext.getAttribute("saleDeptBMs");
        if (saleDeptBMs == null) {
            saleDeptBMs = promotionService.getSaleDeptBM();
            servletContext.setAttribute("saleDeptBMs", saleDeptBMs);
            return saleDeptBMs;
        }
        return saleDeptBMs;
    }

    @GetMapping("/getSaleDeptSBM")
    public List<SaleDeptSBM> getsaleDeptSBM() {
        ServletContext servletContext = webApplicationContext.getServletContext();
        List<SaleDeptSBM> saleDeptSBMs = (List<SaleDeptSBM>) servletContext.getAttribute("saleDeptSBMs");
        if (saleDeptSBMs == null) {
            saleDeptSBMs = promotionService.getSaleDeptSBM();
            servletContext.setAttribute("saleDeptSBMs", saleDeptSBMs);
            return saleDeptSBMs;
        }
        return saleDeptSBMs;
    }

    @GetMapping("/getSaleDeptAS")
    public List<SaleDeptAS> getsaleDeptAS(){
        ServletContext servletContext = webApplicationContext.getServletContext();
        List<SaleDeptAS> saleDeptASs = (List<SaleDeptAS>) servletContext.getAttribute("saleDeptASs");
        if(saleDeptASs==null){
            saleDeptASs = promotionService.getSaleDeptAS();
            servletContext.setAttribute("saleDeptASs",saleDeptASs);
            return saleDeptASs;
        }
        return saleDeptASs;
    }
}
